package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.Result;
import com.itheima.domain.ShoppingCart;
import com.itheima.mapper.ShoppingCartMapper;
import com.itheima.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zhuan
 * @Desc: 购物车模块-业务实现
 * @Date: 2022-05-24 14:36:49
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {


		@Autowired
		private ShoppingCartMapper shoppingCartMapper;
		/**
		 * 功能描述: 菜品、套餐添加购物车
		 * @param shoppingCart
		 * @return : com.itheima.common.Result
		 */
		@Override
		public ShoppingCart add(ShoppingCart shoppingCart) {
				//查询数据库购物车数据
				ShoppingCart dbShoppingCart = null;
				//影响行数--可能是修改影响、新增影响
				int row = 0;
				//1.校验、异常判断
				if(shoppingCart == null){
						return null;
				}
				Long setmealId =  shoppingCart.getSetmealId();
				Long dishId = shoppingCart.getDishId();
				if(setmealId == null && dishId == null){
						return null;
				}
				//2.获取当前登录用户ID
				Long userId = BaseContext.getUserId();
				if(userId == null){
						return null;
				}
				//3、根据userId、dishId、setmealId、dishFlavor查询购物车中菜品是否已经存在，存在则修改数量（number+1）,不存在则新增数据
				LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
				//3.1 添加userId条件-----查询当前用户购物车
				queryWrapper.eq(ShoppingCart::getUserId,userId);
				//3.2 如果是菜品，添加菜品条件；如果是套餐，条件套餐条件
				if(setmealId != null){
						queryWrapper.eq(ShoppingCart::getSetmealId,setmealId);
				}
				if(dishId != null){
						queryWrapper.eq(ShoppingCart::getDishId,dishId);
						//3.3 如果是菜品，还需添加口味条件
						String dishFlavor = shoppingCart.getDishFlavor();
						if(StringUtils.isBlank(dishFlavor)){//菜品必须要有口味
								return null;
						}else{
								queryWrapper.eq(ShoppingCart::getDishFlavor,dishFlavor);
						}
				}
				//3.4 执行查询
				dbShoppingCart = shoppingCartMapper.selectOne(queryWrapper);
				//4 如果数据库中该菜品或套餐已近存在，修改菜品或套餐数量即可
				if(dbShoppingCart != null){
						Integer number = dbShoppingCart.getNumber();
						number = number+1;
						dbShoppingCart.setNumber(number);
						row = shoppingCartMapper.updateById(dbShoppingCart);
				}else {
						shoppingCart.setCreateTime(LocalDateTime.now());
						shoppingCart.setUserId(userId);
						//3.不要使用前端传递的金额，需要自己计算（实际开发）
						//4.添加数据
						dbShoppingCart = shoppingCart;
					  row = shoppingCartMapper.insert(dbShoppingCart);
				}
				if (row == 0) {
						return null;
				}
				return dbShoppingCart;
		}

		/**
		 * 功能描述:  获取当前登录人购物车列表数据
		 * @return : java.util.List<com.itheima.domain.ShoppingCart>
		 */
		@Override
		public List<ShoppingCart> list() {
				List<ShoppingCart> shoppingCartList = null;
				//1.获取当前登录用户ID
				Long userId = BaseContext.getUserId();
				if(userId == null){
						return shoppingCartList;
				}
				//2.封装查询条件
				LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
				wrapper.eq(ShoppingCart::getUserId,userId);
				//根据时间降序排序
				wrapper.orderByDesc(ShoppingCart::getCreateTime);
				//3.执行查询
				shoppingCartList = shoppingCartMapper.selectList(wrapper);
				return shoppingCartList;
		}
}
